package sports.service;

import org.springframework.stereotype.Service;
import sports.dao.entity.SportsRecordPO;
import sports.dao.entity.SportsInfoPO;
import sports.dao.mapper.SportsMapper;
import util.ErrorCode;
import util.ResponseUtil;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.List;
import java.util.ArrayList;

/**
 * Created by qianyang on 2018/4/26.
 */
@Service
public class SportsService {
    @Resource
    private SportsMapper sportsMapper;

    public String selectSportsRecord(Integer userId,Date date){
        System.out.println("in selcetSportsRecord");
        List<SportsRecordPO> sportsRecordPOS=sportsMapper.selcetSportsRecord(userId,date);
        System.out.println(sportsRecordPOS);
        List<SportsRecordVO> sportsRecordVOS=new ArrayList<SportsRecordVO>();
        for(SportsRecordPO sportsRecordPO: sportsRecordPOS){
            SportsRecordVO tmpSportsRecordVO=new SportsRecordVO();
            SportsInfoPO tmpSportsInfoPO=sportsMapper.getSportsInfoById(sportsRecordPO.getSportsId());
            tmpSportsRecordVO.setSportsInfoPO(tmpSportsInfoPO);
            tmpSportsRecordVO.setSportsRecordPO(sportsRecordPO);
            sportsRecordVOS.add(tmpSportsRecordVO);
        }
        if (!sportsRecordPOS.isEmpty()) {
            return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "successful", sportsRecordVOS);
        } else {
            return ResponseUtil.getResponseString(ErrorCode.NOT_EXIST, "运动记录为空", sportsRecordVOS);
        }
    }

    public String selcetALLSports(){
        List<SportsInfoPO> SportsInfoPOS=sportsMapper.selcetALLSportsInfo();
        if (!SportsInfoPOS.isEmpty()) {
            return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "successful", SportsInfoPOS);
        } else {
            return ResponseUtil.getResponseString(ErrorCode.NOT_EXIST, "运动列表为空", SportsInfoPOS);
        }
    }

    public String selcetSportsById(Integer sportsId){
        SportsInfoPO sportsInfoPO=sportsMapper.getSportsInfoById(sportsId);
        if (sportsInfoPO!=null) {
            return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "successful", sportsInfoPO);
        } else {
            return ResponseUtil.getResponseString(ErrorCode.NOT_EXIST, "获取运动信息失败", sportsInfoPO);
        }
    }

    public String selcetSportsRecordById(Integer sportsRecordId){
        SportsRecordPO sportsRecordPO=sportsMapper.getSportsRecordById(sportsRecordId);
        if (sportsRecordPO!=null) {
            return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "successful", sportsRecordPO);
        } else {
            return ResponseUtil.getResponseString(ErrorCode.NOT_EXIST, "获取运动记录失败", sportsRecordPO);
        }
    }

    public String searchSportsByNmae(String name){
        System.out.println("searchDietByNmae");
        List<SportsInfoPO> SportsInfoPOS=sportsMapper.selectSportsInfoByName(name.trim());
        if (!SportsInfoPOS.isEmpty()) {
            return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "successful", SportsInfoPOS);
        } else {
            return ResponseUtil.getResponseString(ErrorCode.NOT_EXIST, "运动列表为空", SportsInfoPOS);
        }
    }

    public String insertSportsRecord(SportsRecordVO sportsRecordVO){
        System.out.println("in insertSportsRecord");
        SportsInfoPO tmpSportsInfo=sportsRecordVO.getSportsInfoPO();
        SportsRecordPO tmpSportsRecord=sportsRecordVO.getSportsRecordPO();
        int userid=tmpSportsRecord.getUserId();
        Date date=tmpSportsRecord.getDate();
        List<SportsRecordPO> sportsRecordPOS=sportsMapper.selcetSportsRecord(userid,date);
        for(SportsRecordPO sportsRecordPO:sportsRecordPOS){
            if(sportsRecordPO.getSportsId()==tmpSportsRecord.getSportsId()){
                System.out.println("insert ,有重复记录");
                SportsRecordVO tmpSportsRecordVO=new SportsRecordVO();
                sportsRecordPO.setAmount(sportsRecordPO.getAmount()+tmpSportsRecord.getAmount());
                sportsRecordPO.setEnergy(sportsRecordPO.getAmount()*tmpSportsInfo.getKcal());
                tmpSportsRecordVO.setSportsRecordPO(sportsRecordPO);
                tmpSportsRecordVO.setSportsInfoPO(tmpSportsInfo);
                System.out.println(tmpSportsRecordVO);
                updateSportsRecord(tmpSportsRecordVO);
                return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "运动记录添加成功(有重复记录)", tmpSportsRecordVO.getSportsRecordPO());
            }
        }
        //System.out.println(tmpDietRecord);
        sportsMapper.insertSportsRecord(tmpSportsRecord);
        return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "运动记录添加成功", tmpSportsRecord);
    }

    public String delectSportsRecord(Integer id){
        sportsMapper.delectSportsRecord(id);
        return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "删除成功", "");
    }

    public String updateSportsRecord(SportsRecordVO sportsRecordVO){
        int id=sportsRecordVO.getSportsRecordPO().getId();
        System.out.println("in update: id="+id);
        System.out.println(sportsRecordVO);
        delectSportsRecord(id);
        insertSportsRecord(sportsRecordVO);

        return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "更新运动记录成功", "");
    }

}
